<template>
  <div class="login-container">
    <div class="login-wrapper">
      <!-- 左侧校徽区域 -->
      <div class="login-left">
        <div class="school-badge">
          <img src="https://img1.baidu.com/it/u=3884491888,4260481962&fm=253&app=138&f=JPEG?w=800&h=800" alt="江西财经大学校徽" class="school-logo">
          <h1 class="school-name">江财现经管</h1>
          <p class="system-title">学生管理系统</p>
        </div>
      </div>

      <!-- 右侧登录/注册区域 -->
      <div class="login-right">
        <el-card class="login-card" shadow="hover">
          <div class="login-header">
            <h2 class="login-title">{{ isRegister ? '用户注册' : '用户登录' }}</h2>
            <p class="login-subtitle">{{ isRegister ? '欢迎注册学生管理系统' : '欢迎使用学生管理系统' }}</p>
          </div>

          <!-- 登录表单 -->
          <el-form
            v-if="!isRegister"
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            label-width="0"
            class="login-form">

            <el-form-item prop="userName">
              <el-input
                v-model="loginForm.userName"
                placeholder="请输入账号"
                size="large"
                prefix-icon="User" />
            </el-form-item>

            <el-form-item prop="passWord">
              <el-input
                v-model="loginForm.passWord"
                type="password"
                placeholder="请输入密码"
                size="large"
                prefix-icon="Lock"
                show-password />
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="login-button"
                :loading="loading"
                @click="handleLogin">
                登录
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 注册表单 -->
          <el-form
            v-else
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            label-width="0"
            class="login-form">

            <el-form-item prop="userName">
              <el-input
                v-model="registerForm.userName"
                placeholder="请输入账号"
                size="large"
                prefix-icon="User" />
            </el-form-item>

            <el-form-item prop="passWord">
              <el-input
                v-model="registerForm.passWord"
                type="password"
                placeholder="请输入密码"
                size="large"
                prefix-icon="Lock"
                show-password />
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="请确认密码"
                size="large"
                prefix-icon="Lock"
                show-password />
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="login-button"
                :loading="loading"
                @click="handleRegister">
                注册
              </el-button>
            </el-form-item>
          </el-form>

          <div class="login-footer">
            <p class="system-info">内部系统 · 仅限授权用户访问</p>
            <div class="switch-mode">
              <span v-if="!isRegister">无账号？</span>
              <span v-else>已有账号？</span>
              <el-link type="primary" @click="toggleMode">
                {{ isRegister ? '立即登录' : '前往注册' }}
              </el-link>
            </div>
          </div>
          <div class="project-info">
            <div class="project-links">
              <div class="project-link" @click="handleOpenProject">
                <el-icon><Link /></el-icon>
                <span>项目地址</span>
              </div>
              <div class="project-link" @click="handleOpenBlog">
                <el-icon><Document /></el-icon>
                <span>个人博客</span>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div></template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {  Link, Document } from '@element-plus/icons-vue'
import { loginRules, registerRules } from '@/miscellaneous'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const router = useRouter()
const loading = ref(false)
const isRegister = ref(false)
const loginFormRef = ref()
const registerFormRef = ref()

// 登录表单数据
const loginForm = ref({
  userName: 'admin',
  passWord: 'admin'
})

// 注册表单数据
const registerForm = ref({
  userName: '',
  passWord: '',
  confirmPassword: ''
})

// 切换登录/注册模式
const toggleMode = () => {
  isRegister.value = !isRegister.value

  // 重置表单数据
  loginForm.value = { userName: '', passWord: '' }
  registerForm.value = { userName: '', passWord: '', confirmPassword: '' }

  // 清除验证状态
  if (loginFormRef.value) {
    loginFormRef.value.clearValidate()
    loginFormRef.value.resetFields()
  }
  if (registerFormRef.value) {
    registerFormRef.value.clearValidate()
    registerFormRef.value.resetFields()
  }
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 登录逻辑
    // console.log('登录信息:', loginForm.value)
    const res = await userStore.loginPinia({
      username: loginForm.value.userName,
      password: loginForm.value.passWord
    })
    if (res.success) {
      ElMessage.success('登录成功')
      // 存储token
      userStore.setTokenPinia(res.data.access)
      router.push('/')
    } else {
      ElMessage.error(res.data.message)
    }
  } catch (error) {
    console.log('登录表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return

  try {
    await registerFormRef.value.validate()
    loading.value = true

    // 注册逻辑
    // console.log('注册信息:', registerForm.value)
    const res = await userStore.registerPinia({
      username: registerForm.value.userName,
      password: registerForm.value.passWord

    })
    if (res.success) {
      ElMessage.success('注册成功')
      toggleMode() // 注册成功后切换到登录
    } else {
      ElMessage.error(res.data.message)
    }
  } catch (error) {
    ElMessage.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 打开项目地址
const handleOpenProject = () => {
  window.open('https://gitee.com/Fan_Fan_Fan8/student-back', '_blank')
}

// 打开个人博客
const handleOpenBlog = () => {
  window.open('http://fanblog.top', '_blank')
}

</script>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #800909 0%, #8b0000 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1200px;
  height: 600px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #800909 0%, #8b0000 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="75" cy="75" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="50" cy="10" r="1" fill="rgba(255,255,255,0.05)"/><circle cx="10" cy="60" r="1" fill="rgba(255,255,255,0.05)"/><circle cx="90" cy="40" r="1" fill="rgba(255,255,255,0.05)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  }
}

.school-badge {
  text-align: center;
  z-index: 1;
  position: relative;

  .school-logo {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 4px solid rgba(255, 215, 0, 0.3);
    padding: 10px;
    background: rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    margin-bottom: 30px;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.05);
    }
  }

  .school-name {
    color: #fff;
    font-size: 32px;
    font-weight: bold;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  }

  .system-title {
    color: rgba(255, 255, 255, 0.9);
    font-size: 18px;
    font-weight: 300;
    letter-spacing: 2px;
  }
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  border: none;
  box-shadow: none;
  background: transparent;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;

  .login-title {
    color: #303133;
    font-size: 28px;
    font-weight: 500;
    margin-bottom: 8px;
  }

  .login-subtitle {
    color: #909399;
    font-size: 14px;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }

  .login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    background: linear-gradient(135deg, #800909 0%, #8b0000 100%);
    border: none;
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(139, 0, 0, 0.3);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

.login-footer {
  text-align: center;
  margin-top: 30px;

  .system-info {
    color: #909399;
    font-size: 12px;
    margin: 0;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    height: auto;
    max-width: 400px;
  }

  .login-left {
    padding: 40px 20px;
    min-height: 200px;

    .school-logo {
      width: 80px;
      height: 80px;
    }

    .school-name {
      font-size: 24px;
    }
  }

  .login-right {
    padding: 30px 20px;
  }
}

// 输入框样式优化
:deep(.el-input) {
  .el-input__wrapper {
    background: #f5f7fa;
    border: 1px solid #e4e7ed;
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
      border-color: #800909;
      background: #fff;
    }

    &.is-focus {
      border-color: #800909;
      background: #fff;
      box-shadow: 0 0 0 2px rgba(139, 0, 0, 0.1);
    }
  }

  .el-input__inner {
    height: 48px;
    font-size: 14px;

    &::placeholder {
      color: #c0c4cc;
    }
  }

  .el-input__prefix {
    color: #909399;
    font-size: 16px;
  }
}
// 项目信息样式
.project-info {
  margin-top: 2rem;
  text-align: center;
}

.project-links {
  display: flex;
  justify-content: center;
  gap: 1rem;
  align-items: center;
}

.project-link {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #909399;
  font-size: 12px;
  transition: all 0.3s ease;
  cursor: pointer;

  .el-icon {
    font-size: 14px;
  }

  span {
    font-size: 12px;
  }

  &:hover {
    color: #800909;
    transform: translateY(-1px);
  }
}
</style>
